const BloomFilter = require("../template")

function testBloomFilterFalsePositive() {
  const bf = new BloomFilter(100, 0.01)

  console.log(
    "1. 假阳性率控制测试:",
    (() => {
      const items = ["item1", "item2", "item3", "item4", "item5"]
      items.forEach(item => bf.add(item))
      const rate = bf.getFalsePositiveRate()
      return rate >= 0 && rate <= 1
    })()
  )

  console.log(
    "2. 假阳性检测测试:",
    (() => {
      const testItems = ["item1", "item2", "item3", "item4", "item5"]
      testItems.forEach(item => bf.add(item))
      
      const existingItems = ["item1", "item2", "item3"]
      const nonExistingItems = ["nonexistent1", "nonexistent2", "nonexistent3"]
      
      const existingResults = existingItems.every(item => bf.contains(item))
      const nonExistingResults = nonExistingItems.some(item => bf.contains(item))
      
      return existingResults === true && nonExistingResults === true // 假阳性是正常的
    })()
  )

  console.log(
    "3. 不同假阳性率测试:",
    (() => {
      const bf1 = new BloomFilter(100, 0.1)
      const bf2 = new BloomFilter(100, 0.01)
      const bf3 = new BloomFilter(100, 0.001)
      
      const items = Array.from({ length: 50 }, (_, i) => `item${i}`)
      items.forEach(item => {
        bf1.add(item)
        bf2.add(item)
        bf3.add(item)
      })
      
      const rate1 = bf1.getFalsePositiveRate()
      const rate2 = bf2.getFalsePositiveRate()
      const rate3 = bf3.getFalsePositiveRate()
      
      return rate1 >= rate2 && rate2 >= rate3
    })()
  )

  console.log(
    "4. 假阳性率随元素数量变化测试:",
    (() => {
      const bf = new BloomFilter(100, 0.01)
      const rates = []
      
      for (let i = 1; i <= 50; i++) {
        bf.add(`item${i}`)
        rates.push(bf.getFalsePositiveRate())
      }
      
      return rates.every((rate, index) => 
        index === 0 || rate >= rates[index - 1]
      )
    })()
  )

  console.log(
    "5. 假阳性率一致性测试:",
    (() => {
      const bf1 = new BloomFilter(100, 0.01)
      const bf2 = new BloomFilter(100, 0.01)
      
      const items = ["item1", "item2", "item3", "item4", "item5"]
      items.forEach(item => {
        bf1.add(item)
        bf2.add(item)
      })
      
      const rate1 = bf1.getFalsePositiveRate()
      const rate2 = bf2.getFalsePositiveRate()
      
      return Math.abs(rate1 - rate2) < 0.001
    })()
  )

  console.log(
    "6. this上下文假阳性测试:",
    (() => {
      const bfObj = {
        threshold: 0.05,
        processFalsePositive: function(items) {
          const bf = new BloomFilter(100, 0.01)
          items.forEach(item => bf.add(item))
          return bf.getFalsePositiveRate() < this.threshold
        }
      }
      return bfObj.processFalsePositive(["a", "b", "c"]) === true
    })()
  )

  console.log(
    "7. 复杂对象this绑定假阳性测试:",
    (() => {
      const bfConfigObj = {
        config: { maxRate: 0.1 },
        processWithConfig: function(items) {
          const bf = new BloomFilter(100, 0.01)
          items.forEach(item => bf.add(item))
          return bf.getFalsePositiveRate() <= this.config.maxRate
        }
      }
      return bfConfigObj.processWithConfig(["a", "b", "c"]) === true
    })()
  )

  console.log(
    "8. 假阳性性能测试:",
    (() => {
      const perfBf = new BloomFilter(1000, 0.01)
      const items = Array.from({ length: 500 }, (_, i) => `item${i}`)
      
      const start = performance.now()
      items.forEach(item => perfBf.add(item))
      const rate = perfBf.getFalsePositiveRate()
      const end = performance.now()
      
      return rate >= 0 && rate <= 1 && (end - start) < 1000
    })()
  )

  console.log(
    "9. 假阳性边界测试:",
    (() => {
      const boundaryBf = new BloomFilter(1, 0.01)
      boundaryBf.add("item")
      const rate = boundaryBf.getFalsePositiveRate()
      return rate >= 0 && rate <= 1
    })()
  )

  console.log(
    "10. 假阳性复杂测试:",
    (() => {
      const complexBf = new BloomFilter(100, 0.01)
      const items = Array.from({ length: 20 }, (_, i) => `item${i}`)
      items.forEach(item => complexBf.add(item))
      
      const rate = complexBf.getFalsePositiveRate()
      const density = complexBf.getBitArrayDensity()
      const estimated = complexBf.estimateItemCount()
      
      return rate >= 0 && rate <= 1 &&
             density >= 0 && density <= 1 &&
             estimated >= 0
    })()
  )
}

testBloomFilterFalsePositive()
